{extend name="./themes/default/index/public/base.html" /}
{block name="content"}
<div id="cart-list" class="allWrap">
	<div class="clearfix">
		<div class="page-title">
			<h1>购物车  (<span id="weight">{$weight}</span>kg ) </h1>
		</div>
		<table class="table">
			<thead>
				<tr>									
					<td  colspan="2" class="text-left">商品名称</td>
					<td class="text-left">货号</td>
					<td class="text-left">数量</td>
					<td class="text-left">单价</td>
					<td class="text-left">总计</td>
					<td class="remove"></td>
				</tr>
			</thead>
			
			<tbody>
				{volist name="goods" id="d"}
					
					<tr class="goods{$d.cart_id}">
						<td style="width:100px;padding:20px;">
							
							<a style="position:relative;display:block;" href="{:url('/goods/'.$d['goods_id'])}"><img src="IMG_ROOT{$d.image}" />
								{if condition="$d['shipping'] eq 0"}
							 		<span class="no_shipping">免配送商品</span>
							 	{/if}
								
							</a>
							
						</td>
						<td >{$d.name}
							<?php if (!$d['stock']) { ?>
							  <span class="stock">***</span>
							  <?php } ?>
							  <div>
								<?php foreach ($d['option'] as $option) { ?>
								<small><?php echo $option['name']; ?>: <?php echo $option['value']; ?></small><br />
								<?php } ?>
			                
							  </div>
						</td>
						<td>{$d.model}</td>
						<td class="quantity" style="width:200px;">
							<div id="select_number{$d.cart_id}" class="select_number">
							  <input class="text" type="text" name="quantity{$d.goods_id}"  onkeyup='change_quantity("{$d.goods_id}",this,"{$d.cart_id}","{$d.cart_id}");' value="{$d.quantity}" size="1" />
								<div>
									<button onclick='changeQty(1,"{$d.goods_id}","{$d.cart_id}","{$d.cart_id}"); return false;' class="increase">+</button>
									<button onclick='changeQty(0,"{$d.goods_id}","{$d.cart_id}","{$d.cart_id}"); return false;' class="decrease">-</button>
								</div>
							</div>						
						</td>		
						
						<td class="price">￥<?php echo round($d['price'] ,2); ?></td>
						<td class="total">￥<?php echo round($d['total'] ,2); ?></td>
						
						<td>
							<a href="{:url('/remove/'.$d['cart_id'])}">
								<img class="btooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="删除" src="__RES__/home/images/remove.png" alt="删除" title="删除" />
							</a>
						</td>
					</tr>
								
				{/volist}
			</tbody>
			
		</table>
		<div class="cart-total ">
		    <table id="total">				      
		      <tr>				
		        <td class="right price last">总计：<?php echo '￥'.$total_price; ?></td>
		      </tr>				      
		    </table>
		 </div>
	     <div id="cart-bottom">					  
		      <a href="{:url('/checkout')}" class="btn checkout">结算</a>
		      <a href="/" class="btn continue">继续购物</a>
	     </div> 
	     
	</div>		
</div>			
{/block}
{block name="javascript"}
<script>	
{/* id:商品id,qty:数量,cart_id:购物车id,key:用于选择标签 */}
function update_cart(id,qty,cart_id,key){
	$.post(
		'{:url("/update")}',
		{id:id,q:qty,cart_id:cart_id},
		function(json){			
			
			if (json['success']) {				
				
				$('#cart a').text(json['success']);
				
				$('#select_number' + key).find("input").val(qty);
				
				$('.goods' + key).find("td.price").text('￥ '+json['price']);
				
				$('.goods' + key).find("td.total").text('￥ '+json['total_price']);
				
				$('.last').text('总计： ￥ '+json['total_all_price']);
				
				$('#weight').text(json['weight']);
				
			}else if(json['error']){
				
				alert(json['error']);			
				
			}	
		}
	);
}

function change_quantity(goods_id,input,cart_id,key){
	var qty=input.value;	
	update_cart(goods_id,qty,cart_id,key);
}

function changeQty(increase,goods_id,cart_id,key) {
    var qty = parseInt($('#select_number' + key).find("input").val());  

    if ( !isNaN(qty) ) {
    	//增加
		if(increase){			
			update_cart(goods_id,(qty+1),cart_id,key);
		}else{
			update_cart(goods_id,(qty-1),cart_id,key);
		}     
    }
}	

</script>
{/block}